from django.utils.deprecation import MiddlewareMixin


class MyMiddleWare(MiddlewareMixin):
    def process_request(self, request):
        print('process_request')
        return None
    def process_view(self, request, callback, callback_args, callback_kwargs):
        print('process_view')
        return None
    def process_response(self, request, response):
        print('process_response')
        if hasattr(response, 'data') and not request.path.endswith('/upload/'):
            formatted_data = {
                'status': response.status_code,
                'message': 'success' if 200 <= response.status_code < 300 else 'fail',
                'data': response.data
            }
            response.data = formatted_data
            response._is_rendered = False
            response.render()
        return response
    def process_exception(self, request, exception):
        print('process_exception')
        return None